package com.base.ui.message
{
	import com.base.Main;
	import com.base.data.DataCenter;
	import com.base.data.message.MessageBean;
	import com.base.util.Utils;
	
	import flash.desktop.NativeApplication;
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	import flashx.textLayout.container.ScrollPolicy;
	
	import mx.containers.Panel;
	import mx.containers.TitleWindow;
	import mx.controls.Button;
	import mx.core.Application;
	import mx.core.FlexGlobals;
	import mx.events.CloseEvent;
	import mx.managers.PopUpManager;
	import mx.managers.SystemManager;
	
	public class MessageOperate extends TitleWindow
	{
		
		protected var bean:MessageBean;
		
		public var addBtn:Button;
		public function MessageOperate()
		{
			showCloseButton = true;
			addEventListener(CloseEvent.CLOSE , onCloseHandler );
		}
		
		protected function onCloseHandler( e:Event ):void {
			remove();
		}
		
		/**
		 * 新增弹出
		 */		
		public function addPopup():void {
			title = "新增帖子";
			
			PopUpManager.addPopUp( this , FlexGlobals.topLevelApplication as Application , true);
			PopUpManager.centerPopUp( this );
			addBtn.visible = false;
		}
		
		/**
		 * 编辑弹出
		 * @param obj
		 */		
		public function editPopup(bean:MessageBean ):void {
			title = "编辑帖子";
			
			this.bean = bean;
			PopUpManager.addPopUp( this , FlexGlobals.topLevelApplication as Application , true);
			PopUpManager.centerPopUp( this );
			
			addBtn.visible = false;
			Utils.convertToUI( this ,bean );
		}
		
		public function remove():void {
			removeEventListener(CloseEvent.CLOSE , onCloseHandler );
			
			PopUpManager.removePopUp( this );
			while(numChildren) removeChildAt( 0 );
			bean = null;
		}
		
		/**
		 * 自动区分保存 更新
		 */		
		public function saveToBean():void {
			if( bean ) updateToBean();
			else addToBean();
		}
		
		/**
		 * 新增保存 
		 */		
		public function addToBean():void {
			bean = new MessageBean();
			Utils.convertToBean( this , bean );
			DataCenter.instance.addMessage( bean );
			remove();
		}
		
		/**
		 * 修改保存
		 */		
		public function updateToBean():void {
			Utils.convertToBean( this , bean );
			DataCenter.instance.updateMessage( bean );
			remove();
		}
	}
}